<?php


/**
 * 获取红包类型列表
 * @access  public
 * @return void
 */
function get_sql_bonus_type_list(){
	$CommonModel	=	new CommonModel();
	
    /* 获得所有红包类型的发放数量 */
    $sql 		= 	"SELECT bonus_type_id, COUNT(*) AS sent_count".
            		" FROM " .'ybr_user_bonus' .
            		" GROUP BY bonus_type_id";
    $res 		= 	M()->query($sql);

    $sent_arr 	= 	array();
    foreach($res as $row){
    	 $sent_arr[$row['bonus_type_id']] = $row['sent_count'];
    }

    /* 获得所有红包类型的发放数量 */
    $sql 		= 	"SELECT bonus_type_id, COUNT(*) AS used_count".
            		" FROM " .'ybr_user_bonus' .
            		" WHERE used_time > 0".
            		" GROUP BY bonus_type_id";
    $res 		= 	M()->query($sql);

    $used_arr 	= 	array();
    
    foreach($res as $row){
    	$used_arr[$row['bonus_type_id']] = $row['used_count'];
    }

    /* 查询条件 */
    $filter['sort_by']    	= 	empty($_REQUEST['sort_by']) ? 'type_id' : trim($_REQUEST['sort_by']);
    $filter['sort_order'] 	=	empty($_REQUEST['sort_order']) ? 'DESC' : trim($_REQUEST['sort_order']);
    
    $sql 					= 	"SELECT * FROM " .'ybr_bonus_type'. " ORDER BY $filter[sort_by] $filter[sort_order]";

    return $sql;
}

/**
 * 获取用户红包列表
 * @access  public
 * @param   $page_param
 * @return void
 */
function get_sql_bonus_list(){
    /* 查询条件 */
    $filter['sort_by']    	= 	empty($_REQUEST['sort_by']) ? 'bonus_type_id' : trim($_REQUEST['sort_by']);
    $filter['sort_order'] 	= 	empty($_REQUEST['sort_order']) ? 'DESC' : trim($_REQUEST['sort_order']);
    $filter['bonus_type'] 	= 	empty($_REQUEST['bonus_type']) ? 0 : intval($_REQUEST['bonus_type']);
    $where 					= 	empty($filter['bonus_type']) ? '' : " WHERE bonus_type_id='$filter[bonus_type]'";

    $sql 					= 	"SELECT ub.*, u.user_name, u.email, o.order_sn, bt.type_name ".
						        " FROM ".'ybr_user_bonus' . " AS ub ".
          						" LEFT JOIN " .'ybr_bonus_type' . " AS bt ON bt.type_id=ub.bonus_type_id ".
          						" LEFT JOIN " .'ybr_users' . " AS u ON u.user_id=ub.user_id ".
          						" LEFT JOIN " .'ybr_order_info' . " AS o ON o.order_id=ub.order_id $where ".
          						" ORDER BY ".$filter['sort_by']." ".$filter['sort_order'];
    return $sql;
}

/**
 * 取得红包类型信息
 * @param   int     $bonus_type_id  红包类型id
 * @return  array
 */
function bonus_type_info($bonus_type_id){
	$CommonModel	=	new CommonModel();
    $sql 			= 	"SELECT * FROM " . 'ybr_bonus_type'  . " WHERE type_id = '$bonus_type_id'";
    return $CommonModel->getRow($sql);
}




/**
 * 取得用户等级数组,按用户级别排序
 * @param   bool      $is_special      是否只显示特殊会员组
 * @return  array     rank_id=>rank_name
 */
function get_rank_list($is_special = false){
    $rank_list 		= 	array();
    $sql 			= 	'SELECT rank_id, rank_name, min_points FROM ' . 'ybr_user_rank';
    if ($is_special){
        $sql 		.= 	' WHERE special_rank = 1';
    }
    $sql 			.= 	' ORDER BY min_points';

    $res 			= 	M()->query($sql);
    
	foreach($res as $row){
		 $rank_list[$row['rank_id']] = $row['rank_name'];
	}
    return $rank_list;
}

/**
 * 查询红包类型的商品列表
 *
 * @access  public
 * @param   integer $type_id
 * @return  array
 */
function get_bonus_goods($type_id){
    $sql 	= 	"SELECT goods_id, goods_name FROM " . 'ybr_goods' . "  WHERE bonus_type_id = '$type_id'";
    $row 	= 	M()->query($sql);
    return $row;
}

/**
 * 取得红包类型数组（用于生成下拉列表）
 *
 * @return  array       分类数组 bonus_typeid => bonus_type_name
 */
function get_bonus_type(){
    $bonus 		= 	array();
    $sql 		= 	'SELECT type_id, type_name, type_money FROM ' . 'ybr_bonus_type' . '  WHERE send_type = 3';
    $res 		= 	M()->query($sql);
	
    foreach($res as $row){
    	 $bonus[$row['type_id']] = $row['type_name'].' [' .sprintf($GLOBALS['_CFG']['currency_format'], $row['type_money']).']';
    }
    return $bonus;
}

/**
 * 添加到邮件模版数据表
 * Enter description here ...
 * @param $username
 * @param $email
 * @param $subject
 * @param $content
 * @param $is_html
 */
function add_to_maillist($username, $email, $subject, $content, $is_html){
	
//	$CommonModel	=	new CommonModel();
//	
//    $time 			= 	time();
//    $content 		= 	addslashes($content);
//    $template_id 	= 	$CommonModel->getOne( "SELECT template_id FROM " . 'ybr_mail_templates'  . "  WHERE template_code = 'send_bonus'");
//    $sql 			= 	"INSERT INTO "  . 'ybr_email_sendlist'  . " ( email, template_id, email_content, pri, last_send) VALUES ('$email', $template_id, '$content', 1, '$time')";
//    $CommonModel->query($sql);
    return true;
}






